import { API_COURSE_URL, API_V1_URL } from '@/config/api';
import {
  AnyObject,
  HandicapRes,
  HttpResponse,
  PageResponse,
} from '@/types/global';
import {
  CourseItem,
  CourseHole,
  CourseApproveItem,
  HandicapCourse,
  HandicapCup,
  CourseCupTee,
} from '@/types/course';
import axios from 'axios';

export function getApproveList(data: AnyObject) {
  return axios.post<HttpResponse<PageResponse<CourseApproveItem>>>(
    `${API_COURSE_URL}/approve/page`,
    data
  );
}

export function getApproveDetail(id: number) {
  return axios.post<HttpResponse<CourseApproveItem>>(
    `${API_COURSE_URL}/approve/detail?id=${id}`
  );
}

export function updateApprove(data: AnyObject) {
  return axios.post(`${API_COURSE_URL}/approve/status`, data);
}

export function getCourseList(data: AnyObject) {
  return axios.post<HttpResponse<PageResponse<CourseItem>>>(
    `${API_COURSE_URL}/page`,
    data
  );
}

export function createDataCourse(name: string) {
  return axios.post(`${API_COURSE_URL}/only-for-data/create`, { name });
}

export function getCourseDetail(id: string) {
  return axios.get<HttpResponse<CourseItem>>(`/api/v1/course/detail?id=${id}`);
}

export function createCourseHole(data: AnyObject[]) {
  return axios.post(`${API_COURSE_URL}/hole/add`, data);
}

export function queryCourseCupTee(courseId: string, cupId: string) {
  return axios.get<HttpResponse<CourseCupTee[]>>(
    `/api/v1/course/tee/query?courseId=${courseId}&cupId=${cupId}`
  );
}

export function queryCourseHole(courseId: string, cupId: string) {
  return axios.get<HttpResponse<CourseHole[]>>(
    `${API_COURSE_URL}/hole/query?courseId=${courseId}&cupId=${cupId}`
  );
}

export function updateCourseHole(data: AnyObject[]) {
  return axios.put(`${API_COURSE_URL}/hole/update`, data);
}

interface HandicapCourseRes extends HandicapRes {
  data?: {
    items?: HandicapCourse[];
  };
}
export function getHandicapCourseList(name: string) {
  return axios.post<HttpResponse<HandicapCourseRes>>(
    `${API_V1_URL}/handicap/course/list?name=${name}`
  );
}

interface HandicapCourseDetailRes extends HandicapRes {
  data: HandicapCourse;
}
export function getHandicapCourseDetail(id: string) {
  return axios.post<HttpResponse<HandicapCourseDetailRes>>(
    `${API_V1_URL}/handicap/course/detail?id=${id}`
  );
}

export function bindHandicapCourse(id: string, officeId: string) {
  return axios.post(`${API_V1_URL}/course/handicap/bind`, {
    id,
    officeId,
  });
}

export function syncHandicapCourseHole(
  courseId: string,
  cupId: string,
  data: {
    courseHoles: Partial<CourseHole>[];
    handicapCourseCupVO?: HandicapCup;
  }
) {
  return axios.put(`${API_V1_URL}/course/hole/sync/handicap`, data, {
    params: {
      courseId,
      cupId,
    },
  });
}

export function createCourseCup(data: AnyObject) {
  return axios.post(`${API_COURSE_URL}/cup/create`, data);
}

export function updateCourseCup(data: AnyObject) {
  return axios.put(`${API_COURSE_URL}/cup/update`, data);
}
